package com.zixuan.gulicollege.eduservice.controller.front;

import com.zixuan.gulicollege.common_utils.R;
import com.zixuan.gulicollege.eduservice.client.VodClient;
import com.zixuan.gulicollege.eduservice.service.EduChapterService;
import com.zixuan.gulicollege.eduservice.service.EduCourseService;
import com.zixuan.gulicollege.entity.eduservice.EduCourse;
import com.zixuan.gulicollege.entity.eduservice.vo.ChapterVO;
import com.zixuan.gulicollege.entity.eduservice.vo.CourseFrontVO;
import com.zixuan.gulicollege.entity.eduservice.vo.CourseInitFrontVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @classname: FrontCourseController
 * @author: zixuan
 * @date: 2021/5/22 18:33
 * @description:
 */
@RestController
@RequestMapping("/edu_service/course_front")
public class FrontCourseController {

    @Autowired
    private EduCourseService courseService;

    @Autowired
    private EduChapterService chapterService;

    @Autowired
    private VodClient vodClient;

    /**
     * 根据讲师id查询课程信息
     *
     * @param id 讲师id
     * @return
     */
    @GetMapping("/getTeacherCourseById/{id}")
    public R getTeacherCourseById(@PathVariable String id) {
        List<EduCourse> teacherCourse = courseService.getTeacherCourseById(id);
        return R.ok().data("teacherCourse", teacherCourse);
    }

    /**
     * 前端根据课程id显示课程信息
     *
     * @param id
     * @return
     */
    @GetMapping("/getCourseFrontById/{id}")
    public R getCourseFrontById(@PathVariable String id) {
        // 查询课程信息
        CourseInitFrontVO courseInfo = courseService.getCourseInfoById(id);

        // 查询章节小结
        List<ChapterVO> chapterVideoList = chapterService.getChapterVideoByCourseId(id);
        return R.ok().data("courseInfo", courseInfo).data("chapterVideoList", chapterVideoList);
    }

    /**
     * 根据条件分页查询前端课程信息
     *
     * @param current
     * @param limit
     * @param courseFrontVO
     * @return
     */
    @PostMapping("/getCourseFrontList/{current}/{limit}")
    public R getCourseFrontList(
            @PathVariable Integer current,
            @PathVariable Integer limit,
            @RequestBody(required = false) CourseFrontVO courseFrontVO) {
        Map<String, Object> map = courseService.getCourseFrontList(current, limit, courseFrontVO);
        return R.ok().data(map);
    }

    /**
     * 根据subjectId获取课程信息
     *
     * @param id
     * @return
     */
    @GetMapping("/getCourseFrontBySubjectId/{id}")
    public R getCourseFrontBySubjectId(@PathVariable String id) {
        List<EduCourse> courseList = courseService.getCourseFrontBySubjectId(id);
        return R.ok().data("courseList", courseList);
    }

    /**
     * 远程调用，根据课程id查询课程信息
     * @param courseId
     * @return
     */
    @GetMapping("/getCourseInfo/{courseId}")
    public CourseInitFrontVO getCourseInfo(@PathVariable String courseId) {
        return courseService.getCourseFrontById(courseId);
    }

    /**
     * 远程调用，修改课程购买信息
     * @param courseId
     * @return
     */
    @PostMapping("/updateCourseBuyCount")
    public boolean updateCourseBuyCount(String courseId) {
        EduCourse course = courseService.getById(courseId);
        Long buyCount = course.getBuyCount();
        course.setBuyCount(++buyCount);
        return courseService.updateById(course);
    }

    @GetMapping("/getPlayAuth/{id}")
    public R getPlayAuth(@PathVariable String id){
        return vodClient.getPlayAuth(id);
    }

}
